export default {
    /**
     * 循环遍历处理树形数据
     *
     * @param {*} data 数据源
     * @param {Function} callback 遍历方法
     * @param {string} [childName='children'] 子节点名称
     * @param {number} [level=1]
     */
    eachTree(data: any, callback: Function, childName:string = 'children', level:number = 1) {
        let current: any, children: any;
        for (let i = 0, len = data.length; i < len; i++) {
            current = data[i];
            // 添加层级
            data[i].level = level;
            children = [];
            if (current[childName] && current[childName].length > 0) {
                children = current[childName];
            }
            callback && callback(current, children);
            if (children.length > 0) {
                this.eachTree(children, callback, childName, level + 1);
            }
        }
    }
}